package com.chuanye.jiguangimdemo.fragments;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.activitys.ChatActivity;
import com.chuanye.jiguangimdemo.activitys.SearchForAddFriendActivity;
import com.chuanye.jiguangimdemo.adapters.MessageAdapter;
import com.chuanye.jiguangimdemo.app.MyApplication;

import java.util.List;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.enums.ConversationType;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;

/**
 * 消息
 */
public class MessageFragment extends BaseFragment implements View.OnClickListener {
    private static final String TAG = "MessageFragment";
    private Context mContext;

    private ImageView message_add_iv;
    private LinearLayout message_view_bg;
    private ListView message_listview_lv;
    private List<Conversation> conversationList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message,null);
        mContext = getActivity();

        initView(view);

        return view;
    }

    /**
     * 初始化控件
     * @param view
     */
    private void initView(View view) {
        message_view_bg = view.findViewById(R.id.message_view_bg);
        message_add_iv = view.findViewById(R.id.message_add_iv);
        message_add_iv.setOnClickListener(this);

        //消息Listview
        message_listview_lv = view.findViewById(R.id.message_listview_lv);


        conversationList = JMessageClient.getConversationList();
        MessageAdapter messageAdapter = new MessageAdapter(mContext, conversationList);
        message_listview_lv.setAdapter(messageAdapter);


        message_listview_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //点击会话条目
                Conversation conversation = conversationList.get(position);
                if (conversation.getType() == ConversationType.single) {
                    Intent intent = new Intent();
                    intent.putExtra(MyApplication.CONV_TITLE, conversation.getTitle());
                    String targetId = ((UserInfo) conversation.getTargetInfo()).getUserName();
                    intent.putExtra(MyApplication.TARGET_ID, targetId);
                    intent.putExtra(MyApplication.TARGET_APP_KEY, conversation.getTargetAppKey());
                    //intent.putExtra(MyApplication.DRAFT, getAdapter().getDraft(conv.getId()));
                    intent.setClass(mContext, ChatActivity.class);
                    mContext.startActivity(intent);
                }

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.message_add_iv://

                initAddPopWindow();

                break;
        }
    }

    @SuppressLint("NewApi")
    private void initAddPopWindow() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.addfriend_popwindow_view,null);
        final PopupWindow mPopWindow = new PopupWindow(view,LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setOutsideTouchable(true);
        //mPopWindow.showAsDropDown(message_add_iv, , 0, 0);
        mPopWindow.showAsDropDown(message_add_iv,0,40,Gravity.BOTTOM);

        //发起单聊
        TextView addfri_pop_danliao_tv = view.findViewById(R.id.addfri_pop_danliao_tv);
        //发起群聊
        TextView addfri_pop_qunliao_tv = view.findViewById(R.id.addfri_pop_qunliao_tv);
        //加好友
        TextView addfri_pop_jiahaoyou_tv = view.findViewById(R.id.addfri_pop_jiahaoyou_tv);
        //加入公开群
        TextView addfri_pop_addqun_tv = view.findViewById(R.id.addfri_pop_addqun_tv);
        //扫一扫
        TextView addfri_pop_saoyisao_tv = view.findViewById(R.id.addfri_pop_saoyisao_tv);

        //发起单聊
        addfri_pop_danliao_tv.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SearchForAddFriendActivity.class);
                intent.setFlags( 2);
                mContext.startActivity(intent);
                if (mPopWindow != null){
                    mPopWindow.dismiss();
                }

            }
        });
        //发起群聊
        addfri_pop_qunliao_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /*Intent intent = new Intent(mContext, CreateGroupActivity.class);
                mContext.startActivity(intent);*/

                if (mPopWindow != null){
                    mPopWindow.dismiss();
                }
            }
        });
        //加好友
        addfri_pop_jiahaoyou_tv.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SearchForAddFriendActivity.class);
                intent.setFlags(1);
                startActivity(intent);
                if (mPopWindow != null){
                    mPopWindow.dismiss();
                }
            }
        });
        //加入公开群
        addfri_pop_addqun_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*Intent intent = new Intent(mContext, SearchAddOpenGroupActivity.class);
                mContext.startActivity(intent);*/

                if (mPopWindow != null){
                    mPopWindow.dismiss();
                }
            }
        });
        //扫一扫
        addfri_pop_saoyisao_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*Intent intent = new Intent(mContext, CommonScanActivity.class);
                intent.putExtra(Constant.REQUEST_SCAN_MODE, Constant.REQUEST_SCAN_MODE_QRCODE_MODE);
                mContext.startActivity(intent);*/


                if (mPopWindow != null){
                    mPopWindow.dismiss();
                }
            }
        });



    }
}
